package ru.ssau.university.web.beans;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import ru.ssau.university.ejb.api.UniversityServiceLocal;
import ru.ssau.university.persistence.entity.Group;
import ru.ssau.university.persistence.entity.PlanItem;
import ru.ssau.university.persistence.entity.Progress;
import ru.ssau.university.persistence.entity.Student;
import ru.ssau.university.web.helper.ListWrapper;
import ru.ssau.university.web.helper.ProgressRow;
import ru.ssau.university.web.helper.ProgressWrapper;

@ManagedBean(name = "progressBean")
@SessionScoped
public class ProgressBean {
	@EJB
	private UniversityServiceLocal universityService;
	
	private Long groupId;
	private Group group;
	private boolean editMode;
	private ListWrapper<PlanItem> planItems = new ListWrapper<PlanItem>();
	private List<ProgressRow> progressRows;
	
	public String initAll() {
		group = universityService.getGroupById(groupId);
		planItems.setItems(universityService.getPlanItems(group.getSpeciality().getId(), group.getTerm()));
		progressRows = new ArrayList<ProgressRow>();
		if (planItems.getId() != null) {
			List<Student> students = universityService.getStudentsByGroup(groupId);
			for (Student s: students) {
				ProgressRow pr = new ProgressRow(s, planItems.getId(), universityService);
				progressRows.add(pr);
			}
		}
		return null;
	}
	
	public String edit() {
		editMode = true;
		return null;
	}
	
	public String save() {
		for (ProgressRow pr: progressRows) {
			for (ProgressWrapper pw: pr.getProgress()) {
				if (!"-".equals(pw.getMark())) {
					saveProgress(pr, pw);
				}
			}
		}
		initAll();
		return null;
	}
	
	private void saveProgress(ProgressRow pr, ProgressWrapper pw) {
		Progress p = pw.getProgress();
		System.out.println("! " + pw.getMark());
		p.setMark(pw.getMark());
		for (PlanItem pi: planItems.getItems()) {
			if (pi.getId() == planItems.getId()) {
				p.setPlanItem(pi);
			}
		}
		p.setNumb(pr.getProgress().indexOf(pw));
		universityService.saveOrUpdateProgress(p);
	}
	
	public String finishEdit() {
		editMode = false;
		return null;
	}

	public Long getGroupId() {
		return groupId;
	}

	public void setGroupId(Long groupId) {
		this.groupId = groupId;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public Group getGroup() {
		return group;
	}

	public boolean isEditMode() {
		return editMode;
	}

	public void setEditMode(boolean editMode) {
		this.editMode = editMode;
	}

	public ListWrapper<PlanItem> getPlanItems() {
		return planItems;
	}

	public void setPlanItems(ListWrapper<PlanItem> planItems) {
		this.planItems = planItems;
	}

	public List<ProgressRow> getProgressRows() {
		return progressRows;
	}

	public void setProgressRows(List<ProgressRow> progressRows) {
		this.progressRows = progressRows;
	}
}
